package com.naiterui.ehp.ps.sms.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.naiterui.ehp.ps.sms.bean.Platform;
import com.naiterui.ehp.ps.sms.cache.SmsRedisManager;
import com.naiterui.ehp.ps.sms.dao.IPlatformDao;
import com.naiterui.ehp.ps.sms.service.IPlatformService;
import com.naiterui.ehp.ps.sms.utils.FilterHtml;
import com.naiterui.ehp.ps.sms.vo.PlatformVO;

import lombok.extern.slf4j.Slf4j;

/**
 * 短信平台service层实现
 *
 * @author
 * @since 1.0.0
 */
@Service
@Slf4j
public class PlatformServiceImpl implements IPlatformService {

    @Autowired
    private IPlatformDao platformDao;

    @Override
    public List<PlatformVO> getAll() {
        List<Platform> platformList = platformDao.getAll();
        List<PlatformVO> platformVOList = new ArrayList<PlatformVO>();
        for (Platform platform : platformList) {
            PlatformVO platformVO = new PlatformVO();
            platformVO.setPlatformName(FilterHtml.xssProtect(platform.getPlatformName()));
            platformVO.setDescription(FilterHtml.xssProtect(platform.getDescription()));
            platformVO.setId(platform.getId());
            platformVO.setStatus(platform.getStatus());
            platformVOList.add(platformVO);
        }
        return platformVOList;
    }

    @Override
    public Integer getCurrentPlatformFromCache() {
        Integer platformIdCached = SmsRedisManager.getCurrentPlatform();
        if (platformIdCached == null) {
            Platform currentPlatform = platformDao.getCurrentPlatform();
            if (currentPlatform != null) {
                platformIdCached = currentPlatform.getId();
                SmsRedisManager.setCurrentPlatform(platformIdCached);
                log.info("回补当前通道缓存信息成功, platformId: {}", platformIdCached);
            }
        }
        return platformIdCached;
    }

}
